import React, { PureComponent, Component } from 'react'
import store from './store'
export function connect(mapStateToProps, mapDispachToprops) {
  return function enhanceHOC(Cpm) {
    return class enhanceCPM extends PureComponent {
      constructor(props) {
        super(props)
        this.state = {
          storeState: mapStateToProps(store.getState()),
        }
      }
      componentDidMount() {
        this.unSubscribe = store.subscribe(() => {
          this.setState({
            storeState: mapStateToProps(store.getState()),
          })
        })
      }
      componentWillUnmount() {
        this.unSubscribe()
      }
      render() {
        return (
          <>
            <Cpm {...this.props} {...mapStateToProps(store.getState())} {...mapDispachToprops(store.dispatch)} />
          </>
        )
      }
    }
  }
}
export function connectMX(mapStateToProps, mapDispachToprops) {
  return function enhanceHOC(Cpm) {
    return class enhanceCPM extends Component {
      componentDidMount() {
        this.unSubscribe = store.subscribe(() => {
          this.setState({})
        })
      }
      componentWillUnmount() {
        this.unSubscribe()
      }
      render() {
        return (
          <>
            <Cpm {...this.props} {...mapStateToProps(store.getState())} {...mapDispachToprops(store.dispatch)} />
          </>
        )
      }
    }
  }
}
export function connectMY(paramsStore) {
  return function (mapStateToProps, mapDispachToprops) {
    return function enhanceHOC(Cpm) {
      return class enhanceCPM extends Component {
        componentDidMount() {
          this.unSubscribe = paramsStore.subscribe(() => {
            this.setState({})
          })
        }
        componentWillUnmount() {
          this.unSubscribe()
        }
        render() {
          return (
            <>
              <Cpm {...this.props} {...mapStateToProps(paramsStore.getState())} {...mapDispachToprops(paramsStore.dispatch)} />
            </>
          )
        }
      }
    }
  }
}
